package com.lancoo.answer.view.result

import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.widget.AppCompatTextView
import androidx.fragment.app.FragmentActivity
import com.lancoo.answer.R
import com.lancoo.answer.model.bean.CognitiveBean
import com.lancoo.answer.view.base.BaseRecyclerAdapter
import com.lancoo.answer.view.base.BaseRecyclerViewHolder

class ResultAnswerSheetChildAdapter(
    val activity: FragmentActivity,
    val cognitiveBean: CognitiveBean,
    var index_A: Int
) : BaseRecyclerAdapter() {

    var indexList: MutableList<String> = ArrayList()
    override fun getHolderView(parent: ViewGroup?, position: Int): View {
        return LayoutInflater.from(activity)
            .inflate(
                R.layout.ev_adapter_answersheet_child, parent,
                false
            )
    }

    override fun onBindViewHolder(
        holder: BaseRecyclerViewHolder,
        position: Int
    ) {
        val mTvSortIndex = holder.convertView.findViewById<AppCompatTextView>(R.id.TvSortIndex)


        var newSortIndex = cognitiveBean.TypeList?.get(index_A)!!.QuesList!![0]
            .SortIndexList!![0].NewSortIndex!!

        if (!newSortIndex.contains("-")) {
            newSortIndex = (newSortIndex.toInt() + position).toString()
        }
        indexList.add(newSortIndex)
        var indexB = positionReturnIndexB(cognitiveBean, position)
        var indexC = positionReturnIndexC(cognitiveBean, position)
        var scoreRate = cognitiveBean.TypeList?.get(index_A)!!.QuesList!![indexB]
            .SortIndexList!![indexC].ScoreRate
        var state = cognitiveBean.TypeList?.get(index_A)!!.QuesList!![indexB]
            .SortIndexList!![indexC].State
        mTvSortIndex.text = newSortIndex.toString()

        if (state == -1 || state == 0) {
            mTvSortIndex.setBackgroundResource(R.mipmap.ev_icon_choice_blue)
        } else {
            if (scoreRate!! >= 0.6) {
                mTvSortIndex.setBackgroundResource(R.mipmap.ev_icon_choice_green)
            } else {
                mTvSortIndex.setBackgroundResource(R.mipmap.ev_icon_choice_red)
            }
        }

        mTvSortIndex.setTextColor(Color.WHITE)

        mTvSortIndex.setOnClickListener {
            var indexC = positionReturnIndexC(cognitiveBean, position)
            var indexB = positionReturnIndexB(cognitiveBean, position)
            clickChilidListener!!.onClick(index_A, indexB, indexC)
        }
    }

    override fun getItemCount(): Int {
        var itemCount = 0
        var quesListSize = cognitiveBean.TypeList?.get(index_A)!!.QuesList?.size
        for (b in 0 until quesListSize!!) {
            itemCount += cognitiveBean.TypeList!![index_A].QuesList!![b].SortIndexList!!.size
        }
        return itemCount
    }

    interface OnClickChildListener {
        fun onClick(index_A: Int, index_B: Int, index_C: Int)
    }

    private var clickChilidListener: OnClickChildListener? = null

    fun setClickChildListener(clickChilidListener: OnClickChildListener?) {
        this.clickChilidListener = clickChilidListener
    }

    private fun positionReturnIndexB(
        cognitiveBean: CognitiveBean,
        position: Int
    ): Int {

        var postionX = -1
        var index_B = 0
        var QuesListSize = cognitiveBean.TypeList?.get(index_A)?.QuesList!!.size
        outLoop@ for (b in 0 until QuesListSize) {
            val sortIndexList = cognitiveBean.TypeList!![index_A].QuesList!![b]
                .SortIndexList!!
            val sortIndexListSize = sortIndexList.size

            for (c in 0 until sortIndexListSize) {
                postionX += 1
                if (indexList[position] == sortIndexList[c].NewSortIndex) {
                    index_B = b
                    break@outLoop
                }
            }
        }
        return index_B
    }
//拿到了点击的位置-》获取点击位置的序号-》遍历数组，匹配序号

    private fun positionReturnIndexC(
        cognitiveBean: CognitiveBean,
        position: Int
    ): Int {
        var postionX = -1
        var index_C = 0
        var QuesListSize = cognitiveBean.TypeList?.get(index_A)?.QuesList!!.size
        outLoop@ for (b in 0 until QuesListSize) {
            val sortIndexList = cognitiveBean.TypeList!![index_A].QuesList!![b]
                .SortIndexList!!
            val sortIndexListSize = sortIndexList.size
            for (c in 0 until sortIndexListSize) {
                postionX += 1
                if (indexList[position] == sortIndexList[c].NewSortIndex) {

                    index_C = c
                    break@outLoop
                }
            }
        }
        return index_C
    }

}